package net.thetabx.ftdba.objects;

import net.thetabx.ftdba.GlobalData;

import android.graphics.drawable.Drawable;
import android.text.Html;
import android.text.Html.ImageGetter;
import android.text.Spanned;

public class Message {
	private int messageId;
	private String time;
	private String userClassId;
	private String userId;
	private String user;
	private Spanned text;
	private static ImageGetter imgGet;
	private static int nTotalMessages = 0;
	
	public Message(String time, String userClassId, String userId, String user, String text) {
		this.messageId = nTotalMessages;
		nTotalMessages++;
		this.time = time;
		this.userClassId = userClassId;
		this.userId = userId;
		this.user = user;
		this.text = processHtml(text);
	}

	public String getTime() {
		return time;
	}
	
	public String getUserClassId() {
		return userClassId;
	}

	public String getUserId() {
		return userId;
	}

	public String getUser() {
		return user;
	}

	public Spanned getText() {
		return text;
	}
	
	public int getId() {
		return messageId;
	}
	
	private Spanned processHtml(String str) {
		return  Html.fromHtml(str, getImageGetter(), null);
		//return Html.fromHtml(str);
	}
	
	private ImageGetter getImageGetter() {
		if(imgGet != null)
			return imgGet;
		
		imgGet = new ImageGetter() {
			public Drawable getDrawable(String source) {
				if(source.indexOf("themes/images/smileys/") != -1) {
					Drawable drawFromPath;
			        int path = GlobalData.getContext().getResources().getIdentifier("net.thetabx.ftdba:drawable/" + source.replaceAll(".*themes/images/smileys/(\\w+).gif", "smiley_$1"), null, null);
			        if(path == 0)
			        	return null;
			        drawFromPath = (Drawable) GlobalData.getContext().getResources().getDrawable(path);
			        drawFromPath.setBounds(0, 0, drawFromPath.getIntrinsicWidth(), drawFromPath.getIntrinsicHeight());
			        return drawFromPath;
				}
				return null;
			}
	    };
	    
	    return imgGet;
	}
}
